<!DOCTYPE html>
<html>

<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!--
Line chart and table with range filter for Quantified Self meeting in London
http://www.meetup.com/LondonQS/events/68812532/

Author silvano.luciani@gmail.com (Silvano Luciani)
-->

  <head>
    <title>Line Chart With Filter</title>
    <script type="text/javascript"
      src='https://www.google.com/jsapi?autoload={
        "modules": [{
          "name": "visualization",
          "version": "1",
          "packages": ["controls"]
        }]
      }'>
    </script>
    <script
      src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
    </script>
    <script type="text/javascript" src="date-en-US.js"></script>
  </head>
  <body>
    <div id="dashboard_div">
      <div id="line_chart_div"></div>
      <div id="filter_div" style="width: 500px; height: 50px; margin-left: 100px"></div>
      <br/><div id="table_chart_div"></div>
    </div>
    <script type="text/javascript">
    $.getJSON('data.json', function(data) {
      
      // The data table for our line chart, table and filter
      var dataTable = new google.visualization.DataTable();
      dataTable.addColumn('datetime', 'Posting Date');
      dataTable.addColumn('number', 'Comments');
      dataTable.addColumn('number', 'Plusones');
      dataTable.addColumn('number', 'Reshares');
      dataTable.addColumn('number', 'EI');
      dataTable.addColumn('string', 'Post Type');
      
      // Extract the data we want to visualize
      $.each(data.items, function(key, val) {
        var date = Date.parseExact(
            val.published, 'yyyy-MM-ddTHH:mm:ss.sssZ');
        // Line below because the date is not always parsed correctly
        if(date) {
          dataTable.addRow([
            date,
            val.object.replies.totalItems,
            val.object.plusoners.totalItems,
            val.object.resharers.totalItems,
            calculateEI(val.object),
            val.verb
          ]);
        }
      });
      
      // We need a dashboard to bind the filter to the line chart and the table
      var dashboard = new google.visualization.Dashboard(
          document.getElementById('dashboard_div'));
      
      // Wrapper for our date range filter
      var slider = new google.visualization.ControlWrapper({
        'controlType': 'ChartRangeFilter',
        'containerId': 'filter_div',
        'options': {
          'filterColumnIndex': 0,
          'ui': {
            'chartType': 'LineChart',
            'chartView': {
              'columns': [0, 4]
            },
          }
        }
      });
      
      // Wrapper for the line chart that shows the engagement on posts
      var wrapper = new google.visualization.ChartWrapper({
        'chartType': 'LineChart',
        'options': {
          'title': 'Engagement on posts',
          'width': 700,
          'height': 300,
          'animation':{
            'duration': 400,
            'easing': 'out'
          },
        },
        'containerId': 'line_chart_div',
        'view': {'columns': [0, 1, 2, 3]}
      });
      
      // Table showing the engagement on posts
      var table = new google.visualization.ChartWrapper({
        'chartType': 'Table',
        'options': {
          'width': 700,
          'height': 300,
          'animation':{
            'duration': 400,
            'easing': 'out'
          },
        },
        'containerId': 'table_chart_div',
        'view': {'columns': [0, 5, 1, 2, 3]}
      });
      
      // We bind the filter to the line chart and the table...
      dashboard.bind(slider, [wrapper, table]);
      // ... and then we draw the dashboard
      dashboard.draw(dataTable);     
    });
    
    // Engagement Index: sum of actions on the post
    // Used in the range filter internal chart to express the trend
    function calculateEI(object) {
      return object.replies.totalItems +
          object.plusoners.totalItems +
          object.resharers.totalItems;   
    }
    </script>
  </body>
</html>